package com.tanhua.sso.server.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.exceptionhander.CustomException;
import com.tanhua.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class AppUserFreezeService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    public void checkUserStatus(String state,Long userId) {
        String value = redisTemplate.opsForValue().get(Constants.FREEZE_USER + userId);
        if(!StringUtils.isEmpty(value)) {
            Map map = JSON.parseObject(value, Map.class);
            String freezingRange = (String) map.get("freezingRange");
            if(state.equals("1")&&"1".equals(freezingRange)) {
                throw new CustomException("账号已冻结");
            }
            if (state.equals("2")&&"2".equals(freezingRange)){
                throw new CustomException("冻结发言");
            }
            if (state.equals("3")&&"3".equals(freezingRange)){
                throw new CustomException("冻结发布动态");
            }
        }
    }
}